package com.songyl.controller;

import com.songyl.entity.Book;
import com.songyl.service.impl.BookServiceImpl;
import com.songyl.util.IsbnTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author syl
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookServiceImpl bookService;
    @Autowired
    IsbnTool isbnTool;

    @GetMapping("/ISBN/{ISBN}")
    @ResponseBody
    public Book getISBN(@PathVariable String ISBN) {

        return bookService.findBookByISBN(ISBN);
    }
    @PutMapping("/ISBN/{ISBN}")
    @ResponseBody
    public Book getBookByISBN(@PathVariable String ISBN) {

        Book book=isbnTool.getBookByISBN(ISBN);
        bookService.save(book);

        return bookService.findBookByISBN(ISBN);
    }

    @RequestMapping("/")
    public ModelAndView  index(Model model) {

        ModelAndView mv = new ModelAndView("book");
        model.addAttribute("book",new Book());
        return mv;
    }

}
